package com.iscas.green.extralindextest.servlet;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义Servlet，通过配置类注册
 */
public class CustomServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(CustomServlet.class);

    @Override
    public void init() throws ServletException {
        log.info("CustomServlet初始化");
        
        // 获取初始化参数
        String paramValue = getInitParameter("paramName");
        log.info("初始化参数paramName={}", paramValue);
        
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        log.info("CustomServlet收到GET请求");
        
        String pathInfo = request.getPathInfo();
        log.info("路径信息: {}", pathInfo);
        
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>CustomServlet示例</h2>");
        response.getWriter().println("<p>这是一个通过配置类注册的Servlet</p>");
        response.getWriter().println("<p>路径信息: " + pathInfo + "</p>");
        response.getWriter().println("<p>当前时间: " + new java.util.Date() + "</p>");
        
        // 显示所有请求参数
        response.getWriter().println("<h3>请求参数:</h3>");
        response.getWriter().println("<ul>");
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String name = paramNames.nextElement();
            String value = request.getParameter(name);
            response.getWriter().println("<li>" + name + " = " + value + "</li>");
        }
        response.getWriter().println("</ul>");
        
        response.getWriter().println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        log.info("CustomServlet收到POST请求");
        
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>POST请求处理结果</h2>");
        
        // 显示所有POST参数
        response.getWriter().println("<h3>POST参数:</h3>");
        response.getWriter().println("<ul>");
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String name = paramNames.nextElement();
            String value = request.getParameter(name);
            response.getWriter().println("<li>" + name + " = " + value + "</li>");
        }
        response.getWriter().println("</ul>");
        
        response.getWriter().println("</body></html>");
    }

    @Override
    public void destroy() {
        log.info("CustomServlet销毁");
        super.destroy();
    }
} 